/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.cemaden.library.model.smp;

import br.gov.cemaden.library.model.BaseEntity;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import org.codehaus.jackson.annotate.JsonManagedReference;

/**
 *
 * @author paulo.dias
 */
@Entity
@Table(name = "smp_pid_aplic", schema = "smp")
@SequenceGenerator(name = "sequence", sequenceName = "smp.smp_pid_aplic_sequence", schema = "smp")
@NamedQueries({
    @NamedQuery(name = "PIDAplic.findAll", query = "SELECT a FROM PIDAplic a ORDER BY a.id ASC"),
    @NamedQuery(name = "PIDAplic.findById", query = "SELECT a FROM PIDAplic a WHERE a.id = :id"),
    @NamedQuery(name = "PIDAplic.findByNomeProcesso", query = "SELECT a FROM PIDAplic a WHERE a.nomeProcesso = :nomeProcesso")
})
public class PIDAplic implements Serializable, BaseEntity {

    @Id
    @GeneratedValue(generator = "sequence", strategy = GenerationType.AUTO)
    @Column(name = "id_pid_aplic", nullable = false, unique = true)
    private Long id;

    @Column(name = "nome_processo")
    private String nomeProcesso;

    /*
     * Esta lista de execuções quer dizer que  tem um nome da aplicação
     * e cada execução tem pid
     * o pid é guardado no exec
     */
    @OneToMany(mappedBy = "pidAplic", cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
    @OrderBy("data DESC")
    @JsonManagedReference
    private Set<ExecAplic> execucoes;

    public PIDAplic() {
        execucoes = new TreeSet<ExecAplic>();
    }

    public Set<ExecAplic> getExecucoes() {
        return execucoes;
    }

    public void setExecucoes(Set<ExecAplic> execucoes) {
        this.execucoes = execucoes;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getNomeProcesso() {
        return nomeProcesso;
    }

    public void setNomeProcesso(String nomeProcesso) {
        this.nomeProcesso = nomeProcesso;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final PIDAplic other = (PIDAplic) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "PIDAplic{" + "id=" + ", nomeProcesso=" + nomeProcesso + '}';
    }
}
